<template>
	<yd-layout id='pay' class=''>
		<header class="header">
			<div class="title">零钱余额</div>
			<div class="money">{{dataObj.money||'0.0'}}</div>
		</header>
		<div class="content">
			<div class="title">
				<div>选择充值零钱金额</div>
				<span>1RMB=1零钱</span>
			</div>
			<ul class="money_list">
				<li @click="num=6" :class="num==6?'list_active':''">6</li>
				<li @click="num=30" :class="num==30?'list_active':''">30</li>
				<li @click="num=60" :class="num==60?'list_active':''">60</li>
				<li @click="num=98" :class="num==98?'list_active':''">98</li>
				<li @click="num=128" :class="num==128?'list_active':''">128</li>
				<li @click="num=188" :class="num==188?'list_active':''">188</li>
			</ul>
			<div class="btn" @click="payMoney">确定充值</div>
		</div>
			
	</yd-layout>
</template>

<script>
	var pingpp = require('pingpp-js');
	export default{
		components:{
			
		},
		data(){
			return {
				num:6,
				dataObj:{}
			}
		},
		created() {
		},
		watch: {
			
		},
		computed: {
			
		},
		mounted(){
			document.title='充值'
			this.getMine()
				
		},
		methods:{
			//请求我的信息
			getMine(){
				this.$root.ajax({
					name:'my',
					params:{
						
					}
				}).then((d)=>{
					d.data.newTime = this.$root.getDate(new Date(d.datamber_exp*1000))
					d.data.money=this.setMoneyZero(d.data,'money')
					d.data.tradeable_money=this.setMoneyZero(d.data,'tradeable_money')
					this.dataObj=Object.assign({},this.dataObj,d.data)
				})
			},
			//处理金钱的0
			setMoneyZero(obj,str){
				if(obj[str]||obj[str]===0){
					if((obj[str]+'').indexOf('.')>=0){
						return obj[str]=obj[str].toFixed(1)
					}else{
						return obj[str] = obj[str] + '.0'
					}
				}
			},
			async payMoney(){
				if(this.$root.config.openId){
					
				}else{
					await this.$root.getOpenId()
				}
				this.$root.ajax({
					name:'my/recharge',
					params:{
						type:0,
						channel:4,
						money:this.num,
						success_url:this.$root.config.openId
						//success_url:window.location.href,
						//success_url:`${this.$root.config.shareUrl}#/success`
					}
				}).then((d)=>{
					d.data.channel='wx_pub'
					pingpp.createPayment(d.data, (result, err)=>{
						// object 需是 Charge/Order/Recharge 的 JSON 字符串
						// 可按需使用 alert 方法弹出 log
						//console.log(JSON.stringify(object));
					    if (result == "success") {
					    	window.location.reload(); 
					    	// 只有微信JSAPI (wx_pub)、微信小程序（wx_lite）、QQ 公众号 (qpay_pub)、支付宝小程序（alipay_lite）支付成功的结果会在这里返回，其他的支付结果都会跳转到 extra 中对应的 URL
					    } else if (result == "fail") {
					    	console.log(err.msg)
					    	this.$dialog.toast({
				                mes: err.msg,
				                timeout: 1500
				            });
					        // Ping++ 对象 object 不正确或者微信JSAPI/微信小程序/QQ公众号支付失败时会在此处返回
						} else if (result == "cancel") {
						   	this.$dialog.toast({
				                mes: '已取消支付！',
				                timeout: 1500
				            });
						        // 微信JSAPI、微信小程序、QQ 公众号、支付宝小程序支付取消支付
					    }
					});
				})
				
			}
		}
	}
</script>

<style lang="scss">
	@import './pay.scss';
</style>